package main;

//import java.applet.*;
import java.awt.*;
import java.awt.event.*;
//import java.util.Random;
import java.util.ArrayList;


//import scenario.*;
import unit.*;
//import equipment.*;

public class UnitSelecter extends PhaseUI {
	
	public Unit goodSelection = null;
	public Unit badSelection = null;
	
	public Label goodStats = new Label("");
	public Label badStats = new Label("");
	
	public Label goodEquipment = new Label("");
	public Label badEquipment = new Label("");
	
	public List goodEquipList = new List(0, true);
	public List badEquipList = new List(0, true);
	
	public List goodUnitList = new List(0, false);
	public List badUnitList = new List(0, false);
	
	public ArrayList<Unit> goodUnits = new ArrayList<Unit>();
	public ArrayList<Unit> badUnits = new ArrayList<Unit>();
	public Army goodArmy;
	public Army badArmy;
	
	public ArrayList<Equipment> goodEquipObjs = new ArrayList<Equipment>();
	public ArrayList<Equipment> badEquipObjs = new ArrayList<Equipment>();
	
	public int currGoodID = 2;
	public int currBadID = 1002;
	
	public static int id = 0;
	
	public UI myUI;
	public UnitPlacer myUP;
	
	public UnitSelecter(UI inputUI){
		myUI = inputUI;
	}
	
	public void beginPhase() {
		myUP = new UnitPlacer(this);
		if(myUI.game.armies[0] != null) {
			this.goodArmy = myUI.game.armies[0];
			this.badArmy = myUI.game.armies[1];
			myUI.phaseUI = myUP;
			myUP.beginPhase();
			return;
		}

		Label goodHeader = new Label(Unit.tabbedHeader());
		goodHeader.setName("header");
		goodHeader.setBounds(25, 180, 150,20);
		Label badHeader = new Label(Unit.tabbedHeader());
		badHeader.setName("header");
		badHeader.setBounds(myUI.dim.width/2+25, 180, 150,20);
		
		goodStats.setBounds(25,200, 150,20);
		goodStats.setName("stats");
		badStats.setBounds(myUI.dim.width/2+25, 200, 150,20);
		badStats.setName("stats");
		
		goodEquipment.setBounds(25,230,myUI.dim.width/2-50,20);
		goodEquipment.setName("stats");
		badEquipment.setBounds(myUI.dim.width/2+25,230,myUI.dim.width/2-50,20);
		badEquipment.setName("stats");
		
		final Choice goodChoice = new Choice();
		goodChoice.setName("goodChoice");
		goodChoice.setBounds(25, 50, 100, 50);
		final Choice badChoice = new Choice();
		badChoice.setName("badChoice");
		badChoice.setBounds(myUI.dim.width/2+25, 50, 100, 50);
		
		goodUnitList.setLocation(25,450);
		goodUnitList.setSize(myUI.dim.width/2-50, 150);
		goodUnitList.setName("army");
		badUnitList.setLocation(myUI.dim.width/2+25,450);
		badUnitList.setSize(myUI.dim.width/2-50, 150);
		badUnitList.setName("army");
		
		// Good units
		final Unit[] goodArray = {new HighElf(0,0,0,0), new WarriorMinasTirith(1,0,0,0)};
		// Bad units
		final Unit[] badArray = {new Goblin(1000,1,0,0), new Orc(1001,1,0,0)};
		
		
		// Populate choices
		for (Unit c : goodArray){
			goodChoice.add(c.name);
		}
		for (Unit c : badArray){
			badChoice.add(c.name);
		}
		
		Button inspectGoodUnit = new Button("Inspect Good Unit");
		inspectGoodUnit.setName("inspect");
		inspectGoodUnit.setBounds(25, 100, 100, 50);
		inspectGoodUnit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Clean up previous equipment stuff
				myUI.removeByName("equipGood");
				
				// Get a new unit for the selected Unit
				//Object[] args = {new Integer(0), new Integer(0), new Integer(0), new Integer(0)};
				try {goodSelection = goodArray[goodChoice.getSelectedIndex()].clone();
					goodSelection.id = currGoodID++;
				} catch (Exception exc) {
					exc.printStackTrace();}
				updateLabels();
				
				// Add new equip stuff
				goodEquipList = new List(0, true);
				goodEquipList.setLocation(25,250);
				goodEquipList.setSize(100, 100);
				goodEquipList.setName("equipGood");
				goodEquipObjs = new ArrayList<Equipment>();
				for (Equipment equip : goodSelection.getEquipChoices()) {
					goodEquipList.add(equip.getClass().getSimpleName());
					goodEquipObjs.add(equip);
				}
				myUI.add(goodEquipList);
				
				Button addGoodUnit = new Button("Add to army");
				addGoodUnit.setName("add");
				addGoodUnit.setBounds(25, 360, 100, 50);
				addGoodUnit.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Unit toAdd = goodSelection.clone();
						toAdd.id = currGoodID++;
						// For each equipment, add to toAdd
						for(int i : goodEquipList.getSelectedIndexes()) {
							try {
								Equipment tempE = goodEquipObjs.get(i).getClass().newInstance();
								toAdd.equip(tempE);
							} catch (InstantiationException e1) {
								e1.printStackTrace();
							} catch (IllegalAccessException e1) {
								e1.printStackTrace();
							}
						}
						goodUnits.add(toAdd);
						goodUnitList.add(toAdd.toChoiceString()+ " - " + toAdd.equipmentString());
					}
				});
				myUI.add(addGoodUnit);
				
				myUI.revalidate();
				myUI.repaint();
				
				
			}
		});
		
		Button inspectBadUnit = new Button("Inspect Bad Unit");
		inspectBadUnit.setName("inspect");
		inspectBadUnit.setBounds(myUI.dim.width/2+25, 100, 100, 50);
		inspectBadUnit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Clean up previous equipment stuff
				myUI.removeByName("equipBad");
				
				// Get a new unit for the selected Unit
				//Object[] args = {new Integer(0), new Integer(0), new Integer(0), new Integer(0)};
				try {badSelection = badArray[badChoice.getSelectedIndex()].clone();
					badSelection.id = currBadID++;
				} catch (Exception exc) {
					exc.printStackTrace();}
				updateLabels();
				
				// Add new equip stuff
				badEquipList = new List(0, true);
				badEquipList.setLocation(myUI.dim.width/2+25,250);
				badEquipList.setSize(100, 100);
				badEquipList.setName("equipBad");
				badEquipObjs = new ArrayList<Equipment>();
				for (Equipment equip : badSelection.getEquipChoices()) {
					badEquipList.add(equip.getClass().getSimpleName());
					badEquipObjs.add(equip);
				}
				myUI.add(badEquipList);
				
				Button addBadUnit = new Button("Add to army");
				addBadUnit.setName("add");
				addBadUnit.setBounds(myUI.dim.width/2+25, 360, 100, 50);
				addBadUnit.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Unit toAdd = badSelection.clone();
						toAdd.id = currBadID++;
						// For each equipment, add to toAdd
						for(int i : badEquipList.getSelectedIndexes()) {
							try {
								Equipment tempE = badEquipObjs.get(i).getClass().newInstance();
								toAdd.equip(tempE);
							} catch (InstantiationException e1) {
								e1.printStackTrace();
							} catch (IllegalAccessException e1) {
								e1.printStackTrace();
							}
						}
						badUnits.add(toAdd);
						badUnitList.add(toAdd.toChoiceString()+ " - " + toAdd.equipmentString());
					}
				});
				myUI.add(addBadUnit);
				
				myUI.revalidate();
				myUI.repaint();
			}
		});
		
		Button place = new Button("Place Units");
		place.setName("place");
		place.setBounds(myUI.dim.width-100, 0, 100, 50);
		place.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				endPhase();
			}
		});
		
		
		
		
		myUI.add(goodChoice);
		myUI.add(badChoice);
		myUI.add(inspectGoodUnit);
		myUI.add(inspectBadUnit);
		myUI.add(goodUnitList);
		myUI.add(badUnitList);
		
		myUI.add(goodHeader);
		myUI.add(badHeader);
		myUI.add(goodStats);
		myUI.add(badStats);
		myUI.add(goodEquipment);
		myUI.add(badEquipment);
		
		myUI.add(place);
		
		myUI.revalidate();
		myUI.repaint();
	}
	
	public void endPhase() {
		// Clean myself and selection up
		myUI.removeByName("place");
		myUI.removeByName("goodChoice");
		myUI.removeByName("badChoice");
		myUI.removeByName("inspect");
		myUI.removeByName("equipGood");
		myUI.removeByName("equipBad");
		myUI.removeByName("add");
		myUI.removeByName("army");
		myUI.removeByName("header");
		myUI.removeByName("stats");
		
		goodArmy = new Army(goodUnits, myUI.game);
		badArmy = new Army(badUnits, myUI.game);
		
		myUI.phaseUI = myUP;
		myUP.beginPhase();
	}
	
	public void updateLabels(){
		if (goodSelection != null) {
			goodStats.setText(goodSelection.tabbedStats());
			goodEquipment.setText("Equipment: " + goodSelection.equipmentString());
		}
		else {
			goodStats.setText("");
			goodEquipment.setText("");
		}
		if (badSelection != null) {
			badStats.setText(badSelection.tabbedStats());
			badEquipment.setText("Equipment: " + badSelection.equipmentString());
		}
		else {
			badStats.setText("");
			badEquipment.setText("");
		}
		myUI.repaint();
	}
	
}